package com.globalwave.common.db.gen;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.globalwave.common.db.MapBean;


public class MapBeanSqlGenerator extends SQLGenerator {

	private Map<String, Object> bean ;
	
	public MapBeanSqlGenerator(Map<String, Object> bean) {
		this.bean = bean ;
	}

    public WHERE getWhere() throws Exception {
                
        WHERE where = new WHERE() ;
        
        for (String key : bean.keySet()) {

            if (key.startsWith(MapBean.INNERT_PROPERTY_PROFIX)) {
                continue ;
            }
            
            final Object fieldValue = bean.get(key) ;
            
            if (fieldValue == null) {
                continue ;
            }
            
            if (where.sql.length() == 0) {
                where.sql.append(" WHERE ") ;
            } else {
                where.sql.append( " AND " ) ;
            }
            
            String rightExp = "?" ;
            List<Object> params = null ;

            
            if (fieldValue instanceof Object[]) {
                Object[] values = ((Object[])fieldValue) ;
                params = new ArrayList<Object>(values.length) ;
                for (int i = 0 ; i < values.length ; i ++) {
                    params.add(values[i]) ;
                    if (i != 0) {
                        rightExp += ",?" ;                        
                    }
                }
                rightExp = "("+ rightExp + ")" ;
            }

            where.sql.append(" " + key + " " + rightExp + " ") ;
            
            if (params == null) {
                where.params.add(fieldValue) ;
            } else {
                where.params.addAll(params) ;
            }
            
        }
        
        return where ;
    }
    
    


    public INSERT getInsert() throws Exception {

        String pkName = (String)bean.get(MapBean.KEY_PK) ;
        if(pkName != null) {// id 未有值
        	//bean.remove("$PK");
        	Long idValue = dao.getNextValue((String)bean.get(MapBean.KEY_PK_SEQ)) ;
        	//bean.remove("$PK_SEQ");
        		
        	bean.put(pkName, idValue) ;
        }

        String tableName = (String)bean.get(MapBean.KEY_TABLE) ;
        //bean.remove("$TABLE") ;
        
        INSERT create = new INSERT() ;
        
        StringBuffer insert = new StringBuffer() ;
        StringBuffer values = new StringBuffer() ;
        
        for (String key : bean.keySet()) {

            if (key.startsWith(MapBean.INNERT_PROPERTY_PROFIX)) {
                continue ;
            }
            
            Object fieldValue = bean.get(key) ;
            
            if (fieldValue == null) {
                continue ;
            }
            
            if (insert.length() == 0) {
                insert.append("INSERT INTO " + tableName + " (") ;
                values.append("VALUES(  ?") ;
            } else {
                insert.append(" , ") ;
                values.append(" , ?") ;
            }
            
            insert.append(key) ;
            
            create.params.add(fieldValue) ;
        }
        
        insert.append(" ) ") ;
        values.append(" ) ") ;
        
        create.sql.append(insert).append(values) ;
        
        return create ;
    }
    


    public UPDATE getUpdate() throws Exception {
        
        UPDATE update = new UPDATE() ;
        String tableName = (String)bean.get(MapBean.KEY_TABLE) ;
        String pk = (String)bean.get(MapBean.KEY_PK) ;
        //bean.remove("$TABLE");
        
        for (String key: bean.keySet()) {

        	if (key.startsWith(MapBean.INNERT_PROPERTY_PROFIX) || key.equals(pk)) {
        		continue ;
        	}
            
            Object fieldValue = bean.get(key) ;
            
            if (update.sql.length() == 0) {
                update.sql.append("UPDATE " + tableName + " SET ") ;
            } else {
                update.sql.append(" , ") ;
            }
            
            update.sql.append(key) ;
            update.sql.append(" = ? ") ;
            
            update.params.add(fieldValue) ;
        }
        
        return update ;
    }
    
    public WHERE getIdWhere() {
        WHERE where = null;
        
        String id = (String)bean.get(MapBean.KEY_PK) ;
        if (id != null) {
            try {
            	Object value = bean.get(id) ;
            	if (value == null) {
            		return where ;
            	}
            	
            	if (where == null) {
            		where = new WHERE() ;
                    where.sql.append(" WHERE ") ;
            	}
                where.params.add(value) ;
            } catch (Exception e) {
            	log.error(e, e) ;
        		return where ;
            }            
            
            where.sql.append(id + " = ? ") ;
        }
        
        return where ;
    }
    
    @Override
    public String getTableName() {
    	return (String)bean.get(MapBean.KEY_TABLE);
    }
    
    public void setBean(Map<String, Object> bean) {
		this.bean = bean;
	}
}
